package com.rd.sys.common.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import baseproj.common.mybatis.page.PageParameter;

import com.opensymphony.xwork2.ActionSupport;
import com.rd.sys.common.web.session.SessionUtil;
import com.rd.sys.dto.commmon.base.BaseDto;
import com.rd.sys.dto.sys.operator.OperatorDto;

/**
 * The Class BaseAction.
 */
public class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {

    //
    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /** The Constant log. */
    protected static final Logger log = LoggerFactory.getLogger(BaseAction.class);

    /** The request. */
    protected HttpServletRequest request;

    /** The response. */
    protected HttpServletResponse response;

    /** The page. */
    protected PageParameter page;

    /** The data list. */
    protected List<? extends BaseDto> dataList;

    /**
     * 从session里获取操作员对象.
     * 
     * @param request
     *            the request
     * @return the operator
     * @return
     */
    protected OperatorDto getOperator(HttpServletRequest request) {
        OperatorDto operator = null;
        try {
            HttpSession session = SessionUtil.getSession(request);
            operator = (OperatorDto) session.getAttribute("Operator");
        } catch (Exception e) {
            log.error("get Operator's info error", e);
        }
        return operator;
    }

    /**
     * 清除浏览器缓存.
     * 
     * @version
     */
    public void clearBrowserCache() {
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
    }

    /**
     * 设置json编码格式.
     * 
     * @version
     */
    protected void setJsonFormat() {
        response.setContentType("text/json; charset=utf-8");
        response.setCharacterEncoding("utf-8");
    }

    /**
     * 设置纯文本编码格式
     * 
     * @version
     */
    protected void setTextFormat() {
        response.setContentType("text/plain; charset=utf-8");
        response.setCharacterEncoding("utf-8");
    }

    /**
     * Sets the servlet request.
     * 
     * @param request
     *            the new servlet request
     */
    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    /**
     * Sets the servlet response.
     * 
     * @param response
     *            the new servlet response
     */
    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }

    /**
     * Gets the page.
     * 
     * @return the page
     */
    public PageParameter getPage() {
        if (null == page) {
            page = new PageParameter();
        }
        return page;
    }

    /**
     * Sets the page.
     * 
     * @param page
     *            the new page
     */
    public void setPage(PageParameter page) {
        this.page = page;
    }

    /**
     * Gets the data list.
     * 
     * @return the dataList
     * @version
     */
    public List<? extends BaseDto> getDataList() {
        return dataList;
    }
}
